home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / IFF / Old_IFF_Packages / July_1992 / apps / 24bitDemo / 24bitDemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-06  |  11.1 KB  |  419 lines

  1. /* 24bitDemo.c 05/91  C. Scheppner CBM
  2.  *
  3.  * Example which creates a 24-bit raster, saves it as a 24-bit ILBM,
  4.  *   then loads it as a brush and shows it to you 4 planes at a time
  5.  *   Optionally (if given a filename) just displays 4 planes at a time.
  6.  *
  7.  * requires linkage with several IFF modules
  8.  * see Makefile
  9.  *
  10.  * 37.10 07/92 - use scr->RastPort.BitMap instead of &scr->BitMap
  11.  *            for future compatibility
  12.  *
  13.  */
  14. #define INTUI_V36_NAMES_ONLY
  15.  
  16. #include "iffp/ilbmapp.h"
  17.  
  18.  
  19. #ifdef LATTICE
  20. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  21. int chkabort(void) { return(0); }  /* really */
  22. #endif
  23.  
  24. void cleanup(void);
  25. void bye(UBYTE *s,int error);
  26.  
  27. #define MINARGS 1
  28. char *vers = "$VER: 24bitDemo 37.10";
  29. char *Copyright = "24bitDemo v37.10 (Freely Redistributable)";
  30. char *usage = "Usage: 24bitDemo [loadname] (saves/loads if no loadname given)";
  31.  
  32.  
  33. struct Library *IntuitionBase  = NULL;
  34. struct Library *GfxBase        = NULL;
  35. struct Library *IFFParseBase   = NULL;
  36.  
  37. /* Note - these fields are also available in the ILBMInfo structure */
  38. struct   Screen         *scr;         /* for ptr to screen structure */
  39. struct   Window         *win;         /* for ptr to window structure */
  40. struct   RastPort       *wrp;         /* for ptr to RastPort  */
  41. struct   ViewPort       *vp;          /* for ptr to Viewport  */
  42.  
  43.  
  44. struct   NewWindow      mynw = {
  45.    0, 0,                                  /* LeftEdge and TopEdge */
  46.    0, 0,                                /* Width and Height */
  47.    -1, -1,                                /* DetailPen and BlockPen */
  48.    IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS, /* IDCMP Flags with Flags below */
  49.    WFLG_BACKDROP | WFLG_BORDERLESS |
  50.    WFLG_SMART_REFRESH | WFLG_NOCAREREFRESH |
  51.    WFLG_ACTIVATE | WFLG_RMBTRAP,
  52.    NULL, NULL,                            /* Gadget and Image pointers */
  53.    NULL,                                  /* Title string */
  54.    NULL,                                  /* Screen ptr null till opened */
  55.    NULL,                                  /* BitMap pointer */
  56.    50, 20,                                /* MinWidth and MinHeight */
  57.    0 , 0,                                 /* MaxWidth and MaxHeight */
  58.    CUSTOMSCREEN                           /* Type of window */
  59.    };
  60.  
  61.  
  62. BOOL   FromWb;
  63.  
  64.  
  65. /* ILBM Property chunks to be grabbed
  66.  * List BMHD, CMAP and CAMG first so we can skip them when we write
  67.  * the file back out (they will be written out with separate code)
  68.  */
  69. LONG    ilbmprops[] = {
  70.         ID_ILBM, ID_BMHD,
  71.         ID_ILBM, ID_CMAP,
  72.         ID_ILBM, ID_CAMG,
  73.         ID_ILBM, ID_CCRT,
  74.         ID_ILBM, ID_AUTH,
  75.         ID_ILBM, ID_Copyright,
  76.         TAG_DONE
  77.         };
  78.  
  79. /* ILBM Collection chunks (more than one in file) to be gathered */
  80. LONG    ilbmcollects[] = {
  81.         ID_ILBM, ID_CRNG,
  82.         TAG_DONE
  83.         };
  84.  
  85. /* ILBM Chunk to stop on */
  86. LONG    ilbmstops[] = {
  87.         ID_ILBM, ID_BODY,
  88.         TAG_DONE
  89.         };
  90.  
  91.  
  92. UBYTE nomem[]  = "Not enough memory\n";
  93. UBYTE noiffh[] = "Can't alloc iff\n";
  94.  
  95.  
  96. /* For our allocated ILBM frames */
  97. struct ILBMInfo  *ilbm[2];
  98.  
  99. #define SCRPLANES 4
  100.  
  101. USHORT colortable[32];
  102. USHORT cstarts[]= { 0x000, 0x800, 0x000, 0x080, 0x000, 0x008 };
  103. USHORT coffs[]    = { 0x100, 0x100, 0x010, 0x010, 0x001, 0x001 };
  104.  
  105. UBYTE *ilbmname = "RAM:24bit.ilbm";
  106. UBYTE *rgbnames[]={"R0","R1","R2","R3","R4","R5","R6","R7",
  107.            "G0","G1","G2","G3","G4","G5","G6","G7",
  108.            "B0","B1","B2","B3","B4","B5","B6","B7" };
  109.  
  110. UBYTE *endtext1 = "Displayed 24 planes, 4 at a time.";
  111. UBYTE *endtext2 = "Press mousebutton or key to exit.";
  112.  
  113. /* 
  114.  * MAIN 
  115.  */
  116. void main(int argc, char **argv)
  117.    {
  118.     struct RastPort *rp = NULL;
  119.     struct BitMap dummy = {0};
  120.     struct BitMap *bm = NULL, *xbm, *sbm;
  121.     LONG    error = 0L;
  122.     USHORT    width, height, depth, pwidth, pheight, pmode, extra, rgb;
  123.     ULONG     plsize;
  124.     UBYTE    *tpp;
  125.     BOOL    DoSave = TRUE;
  126.     int     k, p, s, n;
  127.  
  128.    FromWb = argc ? FALSE : TRUE;
  129.  
  130.    if((argc > 1)&&(argv[argc-1][0]=='?'))
  131.     {
  132.     printf("%s\n%s\n",Copyright,usage);
  133.         bye("",RETURN_OK);
  134.     }
  135.  
  136.    if(argc==2)
  137.     {
  138.     ilbmname = argv[1];
  139.     DoSave = FALSE;
  140.     }
  141.  
  142.    /* Open Libraries */
  143.  
  144.    if(!(IntuitionBase = OpenLibrary("intuition.library", 0)))
  145.       bye("Can't open intuition library.\n",RETURN_WARN);
  146.       
  147.    if(!(GfxBase = OpenLibrary("graphics.library",0)))
  148.       bye("Can't open graphics library.\n",RETURN_WARN);
  149.  
  150.    if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  151.       bye("Can't open iffparse library.\n",RETURN_WARN);
  152.  
  153.  
  154. /* 
  155.  * Alloc ILBMInfo structs
  156.  */
  157.     if(!(ilbm[0] = (struct ILBMInfo *)
  158.     AllocMem(sizeof(struct ILBMInfo),MEMF_PUBLIC|MEMF_CLEAR))) 
  159.         bye(nomem,RETURN_FAIL);
  160.     if(!(ilbm[1] = (struct ILBMInfo *)
  161.     AllocMem(sizeof(struct ILBMInfo),MEMF_PUBLIC|MEMF_CLEAR))) 
  162.         bye(nomem,RETURN_FAIL);
  163.  
  164. /*
  165.  * Here we set up our ILBMInfo fields for our
  166.  * application.
  167.  * Above we have defined the propery and collection chunks
  168.  * we are interested in (some required like BMHD)
  169.  */
  170.  
  171.     ilbm[0]->ParseInfo.propchks        = ilbmprops;
  172.     ilbm[0]->ParseInfo.collectchks    = ilbmcollects;
  173.     ilbm[0]->ParseInfo.stopchks        = ilbmstops;
  174.  
  175.     ilbm[0]->windef    = &mynw;
  176.  
  177.     *ilbm[1] = *ilbm[0];
  178.  
  179.  
  180. /* 
  181.  * Alloc IFF handles for frame
  182.  */
  183.     if(!(ilbm[0]->ParseInfo.iff = AllocIFF())) bye(noiffh,RETURN_FAIL);
  184.     if(!(ilbm[1]->ParseInfo.iff = AllocIFF())) bye(noiffh,RETURN_FAIL);
  185.  
  186.  
  187. /* for saving our demo 24-bit ILBM */
  188.  
  189.     width  = 320;
  190.     height = 200;
  191.     depth  = 24;
  192.  
  193.     /* Page width, height, and mode for saved ILBM */
  194.     pwidth  = width  < 320 ? 320 : width;
  195.     pheight = height < 200 ? 200 : height;
  196.     pmode   = pwidth >= 640  ? HIRES : 0L;
  197.     pmode  |= pheight >= 400 ? LACE  : 0L;
  198.  
  199.     plsize = RASSIZE(width,height);
  200.  
  201.     if(!DoSave)    goto nosave;
  202.  
  203.     /*
  204.      * Allocate Bitmap and planes
  205.      */
  206.     extra = depth > 8 ? depth - 8 : 0;
  207.     if(ilbm[0]->brbitmap = AllocMem(sizeof(struct BitMap) + (extra<<2),
  208.                 MEMF_CLEAR))
  209.     {
  210.     bm = ilbm[0]->brbitmap;
  211.         InitBitMap(bm,depth,width,height);
  212.         for(k=0, error=0; k<depth && (!error); k++) 
  213.             {
  214.             if(!(bm->Planes[k] = AllocRaster(width,height)))
  215.             error = IFFERR_NOMEM;
  216.             if(! error)
  217.         {
  218.                 BltClear(bm->Planes[k], RASSIZE(width,height),0);
  219.                 }
  220.         }
  221.  
  222.     if(!error)
  223.         {
  224.         if(!(rp = AllocMem(sizeof(struct RastPort),MEMF_CLEAR)))
  225.         error = IFFERR_NOMEM;
  226.         else
  227.         {
  228.         InitRastPort(rp);
  229.         rp->BitMap = bm;
  230.         rp->Mask = 0x01;    /* we'll render 1 plane at a time */
  231.         SetAPen(rp,1);
  232.         SetDrMd(rp,JAM1);
  233.         }
  234.         }
  235.  
  236.     if(!error)
  237.         {
  238.         /* Put something recognizable in the planes.
  239.          * Our bitmap is not part of a screen or viewport
  240.          * so we can fiddle with the pointers and depth
  241.          */
  242.         tpp = bm->Planes[0];    /* save first plane pointer */
  243.         bm->Depth = 1;
  244.         for(k=0; k<depth; k++)    /* swap in planeptrs 1 at a time */
  245.         {
  246.         bm->Planes[0] = bm->Planes[k];
  247.         Move(rp,k * 10, (k * 8) + 8);    /* render rgb bitname text */
  248.         Text(rp, rgbnames[k], 2);
  249.         }
  250.         bm->Depth = depth;        /* restore depth */
  251.         bm->Planes[0] = tpp;    /* and first pointer */
  252.  
  253.         /* Save the 24-bit ILBM */
  254.         printf("Saving %s\n",ilbmname);
  255.         error = saveilbm(ilbm[0], ilbm[0]->brbitmap, pmode,
  256.                 width,  height, pwidth, pheight,
  257.                 NULL, 0, 0,    /* colortable */
  258.         mskNone, 0,    /* masking, transparent */
  259.         NULL, NULL,     /* chunklists */
  260.         ilbmname);
  261.         }
  262.  
  263.     /* Free our bitmap */
  264.         for(k=0; k<depth; k++) 
  265.             {
  266.             if(ilbm[0]->brbitmap->Planes[k])
  267.             FreeRaster(ilbm[0]->brbitmap->Planes[k],width,height);
  268.         }
  269.     FreeMem(ilbm[0]->brbitmap, sizeof(struct BitMap) + (extra << 2));
  270.     ilbm[0]->brbitmap = NULL;
  271.     if(rp)    FreeMem(rp, sizeof(struct RastPort));
  272.     }
  273.  
  274.     if(error)
  275.     {
  276.     printf("%s\n",IFFerr(error));
  277.     bye(" ", RETURN_FAIL);
  278.     }
  279.  
  280. nosave:
  281.  
  282. /* Normally you would use showilbm() to open an appropriate acreen
  283.  * and display an ILBM in it.  However, this is a 24-bit ILBM
  284.  * so we will load it as a brush (bitmap).
  285.  * Here we are demonstrating
  286.  *  - first querying an ILBM to get its BMHD and CAMG (real or computed)
  287.  *  - then opening our own display
  288.  *  - then loading the 24-bit ILBM as a brush (bitmap) and displaying
  289.  *    it 4 planes at a time in our 4-plane screen.
  290.  */
  291.  
  292.     printf("Attempting to load %s as a bitmap and display 4 planes at a time\n",
  293.         ilbmname);
  294.  
  295.     if(!(error = queryilbm(ilbm[0],ilbmname)))
  296.     {
  297.     D(bug("24bitDemo: after query, this ILBM is %ld x %ld x %ld, modeid=$%lx\n",
  298.         ilbm[0]->Bmhd.w, ilbm[0]->Bmhd.h, ilbm[0]->Bmhd.nPlanes, ilbm[0]->camg));
  299.  
  300.     /* Note - you could use your own routines to open your
  301.      * display, but if so, you must initialize ilbm[0]->scr,
  302.      * ilbm[0]->win, ilbm[0]->wrp, ilbm[0]->srp, and ilbm[0]->vp for your display.
  303.      * Here we will use opendisplay() which will initialize
  304.      * those fields.
  305.      */
  306.     if(!(opendisplay(ilbm[0],
  307.             MAX(ilbm[0]->Bmhd.pageWidth, ilbm[0]->Bmhd.w),
  308.             MAX(ilbm[0]->Bmhd.pageHeight,ilbm[0]->Bmhd.h),
  309.             MIN(ilbm[0]->Bmhd.nPlanes, SCRPLANES),
  310.             ilbm[0]->camg)))
  311.         {
  312.         printf("Failed to open display\n");
  313.         }
  314.     else
  315.         {
  316.         D(bug("24bitDemo: opendisplay (%ld planes) successful\n",SCRPLANES));
  317.  
  318.         scr = ilbm[0]->scr;
  319.         win = ilbm[0]->win;
  320.         wrp = ilbm[0]->wrp;
  321.         vp  = ilbm[0]->vp;
  322.  
  323.         if(!(error = loadbrush(ilbm[1], ilbmname)))
  324.         {
  325.             D(bug("24bitDemo: loadbrush successful\n"));
  326.  
  327.         /* Note - we don't need to examine or copy any
  328.          * chunks from the file, so we will close file now
  329.          */
  330.         closeifile(ilbm[0]);
  331.         ScreenToFront(ilbm[0]->scr);
  332.  
  333.         xbm = &dummy;            /* spare bitmap */
  334.         sbm = scr->RastPort.BitMap;    /* screen's bitmap */
  335.         bm = ilbm[1]->brbitmap;        /* the 24-plane bitmap */
  336.         depth = bm->Depth;
  337.  
  338.             InitBitMap(xbm,SCRPLANES,scr->Width,scr->Height);
  339.  
  340.         /* Show the 24 planes */
  341.         for(p=0; p<depth; p+=SCRPLANES)    /* 4 at a time */
  342.             {
  343.             SetRast(&scr->RastPort, 0);
  344.             for(s=0; s<SCRPLANES; s++)
  345.             {
  346.             if((p+s) < depth) xbm->Planes[s] = bm->Planes[p+s];
  347.             else            xbm->Planes[s] = NULL, xbm->Depth--;
  348.             }
  349.             /* Blit planes to the screen */
  350.             BltBitMap(xbm, 0, 0,
  351.                   sbm, 0, 0,
  352.                   scr->Width, scr->Height,
  353.                   0xC0, 0x0F, NULL);
  354.  
  355.             /* Emulate 8-bit color with 4-bit per gun colors
  356.              * by using each rgb value twice
  357.              */
  358.             for(n=0, rgb=cstarts[p /SCRPLANES]; n < 16; n++)
  359.             {
  360.             if(!n)    colortable[n] = 0xFFF;
  361.             else    colortable[n] = rgb;
  362.             /* bump gun for every 2 planes since
  363.              * we only have 8 bits per gun
  364.              */
  365.             if(n & 1)  rgb += coffs[ p / SCRPLANES];
  366.             }
  367.             LoadRGB4(vp, colortable, 16);
  368.             Delay(50);
  369.             }
  370.  
  371.         SetRast(&scr->RastPort, 0);
  372.  
  373.         SetAPen(wrp, 1);
  374.         Move(wrp, 24, 80);
  375.         Text(wrp, endtext1, strlen(endtext1));
  376.         Move(wrp, 24, 120);
  377.         Text(wrp, endtext2, strlen(endtext2));
  378.  
  379.         Wait(1<<win->UserPort->mp_SigBit);
  380.         unloadbrush(ilbm[1]);    /* deallocs colors, closeifile if needed */
  381.         }
  382.         closedisplay(ilbm[0]);
  383.         printf("Done\n");
  384.          }
  385.           }
  386.  
  387.     if(error)    printf("%s\n",IFFerr(error));
  388.  
  389.     cleanup();
  390.     exit(RETURN_OK);
  391.     }
  392.  
  393.  
  394. void bye(UBYTE *s,int error)
  395.    {
  396.    if((*s)&&(!FromWb)) printf("%s\n",s);
  397.    cleanup();
  398.    exit(error);
  399.    }
  400.  
  401.  
  402. void cleanup()
  403.    {
  404.    if(ilbm[0])
  405.     {
  406.     if(ilbm[0]->ParseInfo.iff)     FreeIFF(ilbm[0]->ParseInfo.iff);
  407.     FreeMem(ilbm[0],sizeof(struct ILBMInfo));
  408.     }
  409.    if(ilbm[1])
  410.     {
  411.     if(ilbm[1]->ParseInfo.iff)     FreeIFF(ilbm[1]->ParseInfo.iff);
  412.     FreeMem(ilbm[1],sizeof(struct ILBMInfo));
  413.     }
  414.  
  415.    if(GfxBase)              CloseLibrary(GfxBase);
  416.    if(IntuitionBase)     CloseLibrary(IntuitionBase);
  417.    if(IFFParseBase)      CloseLibrary(IFFParseBase);
  418.    }
  419.